package com.lengyue.imakiba.show;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import com.jfinal.core.Controller;
import com.lengyue.imakiba.data.Anime;
import com.lengyue.imakiba.data.Episode;
import com.lengyue.imakiba.data.Type;

public class AnimePlayer extends Controller {
	//百度脚本模板
	private static final String baiduScripts = "<script type=\"text/javascript\" src=\"js/baidu.js\" ></script>"+
	"<script>"+
	"$(function(){"+
		"$(\"#player\").baiduPlayer({"+
			"url:\"#url#\","+
			"height:495,"+
			"width:720,"+
			//上一集页面地址
			"lastWebPage:\"#lastWebPage#\","+
			//下一集页面地址
			"nextWebPage:\"#nextWebPage#\","+
			//下一集缓冲播放源地址
			"nextCacheUrl:\"#nextUrl#\","+
		"});"+
	"});"+
	"</script>";
	//快播脚本模板
	private static final String qvodScripts = "<script type=\"text/javascript\" src=\"js/jquery.qvod.js\" ></script>"+
	"<script type=\"text/javascript\">"+
	"$(function(){"+
		"$(\"#player\").qvod({"+
			"width:\"720\","+
			"height:\"495\","+
			"QvodUrl:\"#url#\","+
			"NextQvod:\"#nextUrl#\","+
			"NextWebPage:\"#nextWebPage#\","+
		"});"+
	"});"+
	"</script>";
	//上一集或下一集地址模板
	private static final String nextOrLastUrl = "animePlayer?animeId=#animeId#&episodeId=#episodeId#&urlName=#urlName#";
	//优酷播放模板
	private static final String youkuPlayer = "<iframe class=\"player\" src=\"#url#\" frameborder=0 allowfullscreen>";
	//土豆播放模板
	private static final String tudouPlayer = "<embed class=\"player\" src=\"#url#\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" wmode=\"opaque\"></embed>";
	//乐视播放模板
	private static final String letvPlayer = "<object class=\"player\">"+
						"<param name='allowFullScreen' value='true'>"+
						"<param name='movie' value='http://i7.imgs.letv.com/player/swfPlayer.swf?autoPlay=1&id=#url#'/>"+
						"<embed src='http://i7.imgs.letv.com/player/swfPlayer.swf?autoPlay=1&id=#url#' class=\"player\" allowFullScreen='true' type='application/x-shockwave-flash'/>"+
					"</object>";
	//腾讯视频播放模板
	private static final String qqPlayer = "<embed class=\"player\" src=\"#url#\" allowFullScreen=\"true\" quality=\"high\" align=\"middle\" allowScriptAccess=\"always\" type=\"application/x-shockwave-flash\"></embed>";
	
	
	public void index()
	{
		int animeId = getParaToInt("animeId");
		int episodeId = getParaToInt("episodeId");
		String urlName = getPara("urlName");
		Anime anime = Anime.dao.findById(animeId);
		Episode episode = Episode.dao.findById(episodeId);
		//当前播放url
		String url = episode.getStr(urlName);
		//下一集url
		String nextUrl;
		//下一集动漫Id
		int nextEpisodeId = 0;
		try {
			Episode nextEpisode = Episode.dao.find("select "+urlName+",id from episode where anime_id = ? and num = ?",animeId,episode.getInt("num").intValue()+1).get(0);
			nextUrl = nextEpisode.getStr(urlName);
			nextEpisodeId = nextEpisode.getInt("id");
		} catch (Exception e) {
			nextUrl = "";
		}
		//上一集动漫Id
		int lastEpisodeId = 0;
		try {
			Episode laseEpisode = Episode.dao.find("select "+urlName+",id from episode where anime_id = ? and num = ?",animeId,episode.getInt("num").intValue()-1).get(0);
			lastEpisodeId = laseEpisode.getInt("id");
		} catch (Exception e) {
		}
		List<Episode> episodes = Episode.dao.find("select * from episode where anime_id = ? order by num", animeId);
		//取动漫urlName对应的数据
		ArrayList<HashMap<String,String>> urls = AnimeInfo.getEpisodeUrls(episodes,urlName).get(urlName);
		//动漫类型
		List<Type> types = Type.dao.find("select * from type order by sort_no");
		String scripts = getScripts(animeId, urlName, url, nextUrl, nextEpisodeId, lastEpisodeId);
		String player = getPlayer(urlName, url);
		setAttr("scripts", scripts);
		setAttr("player", player);
		setAttr("urls", urls);
		setAttr("types", types);
		setAttr("anime", anime);
		setAttr("episode", episode);
		//返回urlName用于设置下一集页面
		setAttr("urlName",urlName);
		render("/WEB-INF/freeMarkerTemplete/player.ftl");
	}
	/**
	 * 取播放控件脚本，现只有百度，优酷需要
	 * @param animeId 动漫id
	 * @param urlName 动漫类型名
	 * @param url 动漫资源地址
	 * @param nextUrl 下一集资源地址
	 * @param nextEpisodeId 下一集id
	 * @param lastEpisodeId 上一集id
	 * @return 播放控件脚本，没有则返回空字符串
	 */
	private String getScripts(int animeId,String urlName,String url,String nextUrl,int nextEpisodeId,int lastEpisodeId)
	{
		if("url1".equals(urlName))
		{
			return baiduScripts.replace("#animeId#", animeId+"").replace("#urlName#", urlName).replace("#url#", url).replace("#nextUrl#", nextUrl).replace("#nextWebPage#", getNextOrLastUrl(animeId, nextEpisodeId, urlName)).replace("#lastWebPage#", getNextOrLastUrl(animeId, lastEpisodeId, urlName));
		}
		if("url2".equals(urlName))
		{
			return qvodScripts.replace("#animeId#", animeId+"").replace("#urlName#", urlName).replace("#url#", url).replace("#nextUrl#", nextUrl).replace("#nextWebPage#", getNextOrLastUrl(animeId, nextEpisodeId, urlName)).replace("#lastWebPage#", getNextOrLastUrl(animeId, lastEpisodeId, urlName));
		}
		return "";
	}
	/**
	 * 取播放控件
	 * @param urlName 动漫类型名
	 * @param url 动漫资源地址
	 * @return 播放控件，没有则返回空字符串
	 */
	private String getPlayer(String urlName,String url)
	{
		if("url3".equals(urlName))
		{
			return youkuPlayer.replace("#url#", url);
		}
		if("url4".equals(urlName))
		{
			return tudouPlayer.replace("#url#", url);
		}
		if("url5".equals(urlName))
		{
			return letvPlayer.replace("#url#", url);
		}
		if("url6".equals(urlName))
		{
			return qqPlayer.replace("#url#", url);
		}
		return "";
	}
	/**
	 * 取上一集或下一集页面地址
	 * @param animeId 动漫id
	 * @param episodeId 剧集id
	 * @param urlName 路径类型
	 * @return
	 */
	private String getNextOrLastUrl(int animeId,int episodeId,String urlName)
	{
		if(episodeId==0)
		{
			return "";
		}else
		{
			return nextOrLastUrl.replace("#animeId#", animeId+"").replace("#episodeId#", episodeId+"").replace("#urlName#", urlName);
		}
	}
}
